package com.swq.java.code.chapter13.code30;

/**
 *  为了和书中标号对上，这里直接用code30
 */
public class Machine extends Thread {
    public Machine(ThreadGroup group, String name) {
        super(group, name);
    }

    public void run() {
        for (int a = 0; a < 1000; a++) {
            System.out.println(Thread.currentThread().getThreadGroup().getName() + ":" + Thread.currentThread().getName() + ":" + a);
            yield();
        }
    }

    public static void main(String args[]) throws Exception {
        ThreadGroup group = new ThreadGroup("machines");

        for (int i = 1; i <= 5; i++) {
            Machine machineTmp = new Machine(group, "machine" + i);
            machineTmp.start();
        }

        int activeCount = group.activeCount();
        Machine machine = new Machine(group, "machine" + 6);
        machine.start();
        Thread[] machines = new Thread[activeCount];
        group.enumerate(machines);
        for (int i = 0; i < activeCount; i++) {
            System.out.println(machines[i].getName() + " is alive");
        }
    }
}